package escenario;

public class CollisionDetector implements Runnable {

	private Escenario es;
	
	public CollisionDetector (Escenario es){
		setEs(es);
	}
	
	private static double truncate(double x){
	    long y=(long)(x*100);
	    return (double)y/100;
	}
	
	public Escenario getEs() {
		return es;
	}

	public void setEs(Escenario es) {
		this.es = es;
	}
	
	public boolean calculoColision(IdPosEnemigo a, IdPosEnemigo b){
		double temp1 = StrictMath.abs((a.getX()-b.getX()));
		double temp2 = StrictMath.abs((a.getY()-b.getY()));
		double temp3 = StrictMath.abs((a.getZ()-b.getZ()));
		
		if(temp1 < 2 && temp2 < 2 && temp3 < 2)
			return true;
		else
			return false;	
	}
	public void calculoColisionTotal(){
		
		if((this.getEs().getSizeArrayPosEnemigos()) > 1){
		
			for(int i=0; i < ((this.getEs().getSizeArrayPosEnemigos())-1); i++){
				
				IdPosEnemigo temp1 = this.getEs().getPosEnemigo(i);
				
				for(int j=(i+1); j < (this.getEs().getSizeArrayPosEnemigos()); j++){
						
					IdPosEnemigo temp2 = this.getEs().getPosEnemigo(j);
							
					if(calculoColision(temp1, temp2)){
						
//							System.out.println("#####IMPACTO: " + temp1.getId() + " - " + temp2.getId());
						
						temp2.getME().colision();
						temp1.getME().colision();
						
					}
				
				}
			}
		}
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
		for(int i=0; i<this.getEs().getSizeArrayPosEnemigos(); i++){
				System.out.print(("ID:" + truncate(this.getEs().getPosEnemigo(i).getId()) + " X:"
						+ truncate(this.getEs().getPosEnemigo(i).getX()) + " Y:" 
						+ truncate(this.getEs().getPosEnemigo(i).getY()) + " Z:"
						+ truncate(this.getEs().getPosEnemigo(i).getZ()) + "        "));
			}
			calculoColisionTotal();
			System.out.println();
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
}
